package com.wifiin.springboot.webmagic.magic;

import java.io.IOException;

import javax.management.JMException;

import com.wifiin.log.LoggerFactory;
import com.wifiin.springboot.webmagic.constant.SougouConstantData;
import com.wifiin.springboot.webmagic.conversion.CreateTxtFile;

import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.monitor.SpiderMonitor;

@Component
public class StartMagic implements CommandLineRunner {
	private static LoggerFactory.Logger logger = LoggerFactory.getLogger(CreateTxtFile.class);
	private CreateTxtFile createTxtFile = new CreateTxtFile();

	@Override
	public void run(String... args) {
		logger.info("magic start...");
		String url = SougouConstantData.SOUGOU_MAGIC_URL;
		Spider sougouSpider = Spider.create(new SougouDictPageProcessor()).addUrl(url).thread(30);
		try {
			SpiderMonitor.instance().register(sougouSpider);
		} catch (JMException e) {
			logger.warn("magic exception:{}",e.getMessage());
		}
		sougouSpider.start();
		while (true) {
			if (sougouSpider.getStatus().equals(Spider.Status.Stopped)) {
				try {
					createTxtFile.writeToTargetFile(SougouConstantData.SOUGOU_TXT_FILE_SRC);
				} catch (IOException e) {
					logger.warn(e.getMessage());
				} finally {
					logger.info("magic end...");
					break;
				}
			}
		}
	}
}
